package com.quotes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import org.apache.log4j.Logger;

public class Parameters implements Cloneable
{
	private TreeMap<String, Object> parameters = new TreeMap<String, Object>();
	
	public double getAsDouble(String key) {
		return (Double)parameters.get(key);
	}
	
	public int getAsInt(String key) {
		return (Integer)parameters.get(key);
	}
	
	public void set(String key, Object value) {
		parameters.put(key, value);
	}
	
	public boolean getAsBoolean(String key) {
		return (Boolean)parameters.get(key);
	}
	
	public String getAsString(String key) {
		return (String)parameters.get(key);
	}
	
	public int[] getAsIntArray(String key) {
		List<Integer> result = new ArrayList<Integer>();
		String elements = getAsString(key);
		
		for (String element : elements.split("-")) {
			result.add(new Integer(element));
		}
		
		int[] arrayResult = new int[result.size()];
		for (int i=0; i<result.size(); i++) {
			arrayResult[i] = result.get(i).intValue();
		}

		return arrayResult;
	}
	
	public Set<Entry<String, Object>> entrySet() {
		return parameters.entrySet();
	}
	
	public Parameters clone() {
		Parameters result = new Parameters();
		
		for (Entry<String, Object> entry : parameters.entrySet()) {
			Object obj = entry.getValue();
			if (obj instanceof Double) {
				double value = (Double)entry.getValue();
				result.set(entry.getKey(), value);
			} else if (obj instanceof Integer) {
				int value = (Integer)entry.getValue();
				result.set(entry.getKey(), value);
			} else if (obj instanceof Boolean) {
				boolean value = (Boolean)entry.getValue();
				result.set(entry.getKey(), value);
			} else if (obj instanceof String) {
				String value = entry.getValue().toString();
				result.set(entry.getKey(), value);
			}
		}

		return result;
	}
	
//	public String toString() {
//		String result = "";
//		
//		for (Entry<String, Object> entry : parameters.entrySet()) {
//			result += entry.getKey() + "=" + entry.getValue().toString() + ",";
//		}
//		
//		return result;
//	}
}
